package com.gs.zsmanage.service.impl;

import com.gs.zsmanage.dao.CustomLiveMapper;
import com.gs.zsmanage.entity.CustomLive;
import com.gs.zsmanage.entity.Element;
import com.gs.zsmanage.service.CustomLiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomLiveServiceImpl implements CustomLiveService {

    @Autowired
    CustomLiveMapper customLiveMapper;

    @Override
    public CustomLive getCustomLiveByDepartmentAndStationType(String department, String stationType) {
        CustomLive customLive = customLiveMapper.listCustomLiveByDepartmentAndStationType(department, stationType);

        Map<String, List<Element>> map = new HashMap<>();
        List<Integer> elementIds = new ArrayList<>();
        if (customLive != null) {
            List<Element> elements = customLiveMapper.listLiveElementsByStationTypeAndLiveId(stationType, customLive.getId());
            if (elements != null)
                for (Element element : elements) {
                    elementIds.add(element.getId());
                    if (!map.containsKey(element.getType())) {
                        List<Element> temp = new ArrayList<>();
                        temp.add(element);
                        map.put(element.getType(), temp);
                    } else {
                        map.get(element.getType()).add(element);
                    }
                }
            customLive.setElements(map);
            customLive.setElementIds(elementIds);
        }
        List<Element> allElements = customLiveMapper.listAllElementsByStationType(stationType);
        Map<String, List<Element>> allMap = new HashMap<>();
        if (allElements != null)
            for (Element element : allElements) {
                if (!allMap.containsKey(element.getType())) {
                    List<Element> temp = new ArrayList<>();
                    temp.add(element);
                    allMap.put(element.getType(), temp);
                } else {
                    allMap.get(element.getType()).add(element);
                }
            }
        customLive.setAllElements(allMap);
        return customLive;
    }

    @Override
    public void addLiveElement(Integer liveId, Integer elementId) {
        customLiveMapper.addLiveElement(liveId, elementId);
    }

    @Override
    public void deleteLiveElement(Integer liveId, Integer elementId) {
        customLiveMapper.deleteLiveElement(liveId, elementId);
    }

    @Override
    public void updateLiveStations(Integer liveId, String stations) {
        customLiveMapper.updateLiveStations(liveId, stations);
    }

    @Override
    public List<CustomLive> getAllDepartmentsAndStationType() {
        return customLiveMapper.listAllDepartmentAndStationType();
    }

    @Override
    public void updateVisible(Integer liveId, Integer v) {
        customLiveMapper.updateVisible(liveId, v);
    }
}